{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from functools import reduce"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "text = 'Premature optimization is the root of all evil -- DonaldKnuth'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, j, k = 0, 0, set()\n",
    "    bi, bj = 0, 0\n",
    "    \n",
    "    while j < len(sequence):\n",
    "        if sequence[j] in k:\n",
    "            k.remove(sequence[i])\n",
    "            i += 1\n",
    "        else:\n",
    "            k.add(sequence[j])\n",
    "            j += 1\n",
    "\n",
    "        if j - i > bj - bi:\n",
    "            bi, bj = i, j\n",
    "\n",
    "    return bi, bj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 12 \"mature op\"\n"
     ]
    }
   ],
   "source": [
    "i, j = longest_unique_sequence(text)\n",
    "print(i, j, '\"%s\"' % text[i:j])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, j = 0, 0\n",
    "    bi, bj = 0, 0\n",
    "    \n",
    "    while j < len(sequence):\n",
    "        if sequence[j] in sequence[i:j]:\n",
    "            i += 1\n",
    "        else:\n",
    "            j += 1\n",
    "    \n",
    "        if j - i > bj - bi:\n",
    "            bi, bj = i, j\n",
    "            \n",
    "    return bi, bj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 12 \"mature op\"\n"
     ]
    }
   ],
   "source": [
    "i, j = longest_unique_sequence(text)\n",
    "print(i, j, '\"%s\"' % text[i:j])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, j = 0, 0\n",
    "    b = 0, 0, 0\n",
    "    \n",
    "    while j < len(sequence):\n",
    "        k = sequence[j] in sequence[i:j]\n",
    "        i, j = i + k, j + 1 - k\n",
    "        b = max(b, (j - i, i, j))\n",
    "\n",
    "    return slice(b[1], b[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "slice(48, 57, None) \"- DonaldK\"\n"
     ]
    }
   ],
   "source": [
    "i = longest_unique_sequence(text)\n",
    "print(i, '\"%s\"' % text[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, b = 0, ''\n",
    "    \n",
    "    while i < len(sequence):\n",
    "        if sequence[i] in sequence[:i]:\n",
    "            i -= 1\n",
    "            sequence = sequence[1:]\n",
    "        else:\n",
    "            i += 1\n",
    "            b = max(b, sequence[:i], key=len)\n",
    "\n",
    "    return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mature op'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "longest_unique_sequence(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, j = 0, 0\n",
    "\n",
    "    while j < len(sequence):\n",
    "        if sequence[j] in sequence[i:j]:\n",
    "            i += 1\n",
    "        else:\n",
    "            j += 1\n",
    "            yield sequence[i:j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mature op'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(longest_unique_sequence(text), key=len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## version 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence):\n",
    "    i, k = 0, {}\n",
    "\n",
    "    for j, x in enumerate(sequence):\n",
    "        i = max(i, k.get(x, 0))\n",
    "        k[x] = j + 1\n",
    "        yield sequence[i:j + 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mature op'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(longest_unique_sequence(text), key=len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## version 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def longest_unique_sequence(sequence, best=''):\n",
    "    for x in sequence:\n",
    "        best = best[best.find(x) + 1:] + x\n",
    "        yield best"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mature op'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(longest_unique_sequence(text), key=len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
